﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EmitMapper;
using EmitMapper.MappingConfiguration;

namespace EmitMapperConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Sourse src = new Sourse
            {
                A = 1,
                //B = 0M,
                C = "2011/9/21 0:00:00",
                D = new Inner
                {
                    D1 = new Random().Next(10),
                    D2 = Guid.NewGuid()
                },
                E = "test"
            };

            ObjectsMapper<Sourse, Dest> mapper1 =
        new ObjectMapperManager().GetMapper<Sourse, Dest>(
            new DefaultMapConfig()
            .IgnoreMembers<Sourse, Dest>(new[] { "A" })//忽略字段A映射
            .NullSubstitution<decimal?, decimal>((value) => -1M)//decimal类型为NULL的设置默认值为-1
            .NullSubstitution<DateTime?, DateTime>((value) => DateTime.Now)//DateTime类型为NULL的设置默认值为当前时间
            .NullSubstitution<string, string>((value) => "")//string类型为NULL的设置默认值为空
            .ConvertUsing<Inner, Inner2>(value => new Inner2 { D12 = value.D1, D22 = value.D2 })//指定实体进行转换
            .MatchMembers((x, y) =>
             {
                 if (x == "E" && y == "F")
                 {
                     return true;
                 }
                 return x == y;
             })//将E映射成F
               .MatchMembers((x, y) =>
               {
                   if (x == "D" && y == "Code")
                   {
                       return true;
                   }
                   return x == y;
               })//将D映射成Code,为下一行取D的值做准备
                .ConvertUsing<Inner, string>(value => value.D2.ToString())//指定实体进行转换
            .PostProcess<Dest>((value, state) => { value.F = "nothing" + value.F; return value; })//映射结束后对字段进行特殊处理
                //E转F
            );
            Dest dst = mapper1.Map(src);
        }
    }

    public class Sourse
    {
        public int A;
        public decimal? B;
        public string C;
        public Inner D;
        public string E;
        public Inner Code;
    }

    public class Dest
    {
        public int? A;
        public decimal B;
        public DateTime C;
        public Inner2 D;
        public string F;
        public string Code;
    }

    public class Inner
    {
        public long D1;
        public Guid D2;
    }

    public class Inner2
    {
        public long D12;
        public Guid D22;
    }
}
